#include "TNode.h"
#include <iostream>

TNode::TNode(ElemType data)
{
	const char* s = data.c_str();	//string->const char*
	char* ch = new char[strlen(s) + 1];//new char[]
	std::strcpy(ch,s);
	this->word = ch;
	this->left = NULL;
	this->right = NULL;
	this->count = 1;
}

TNode::~TNode()
{
}

TNode* TNode::getLeft()
{
	return left;
}

TNode* TNode::getRight()
{
	return right;
}

void TNode::addTNode(ElemType elem)
{
	if (elem<word)
	{
		if (left == NULL) {
			left = new TNode(elem);
		}
		else
		{
			left->addTNode(elem);
		}
	}
	if (elem>word)
	{
		if (right == NULL) {
			right = new TNode(elem);
		}
		else
		{
			right->addTNode(elem);
		}
	}
	if (elem==word)
	{
		count++;
	}
}

void TNode::inTaversal()
{
	if (left!=NULL)	left->inTaversal();
	for (int i = 0; i < count; i++)
	{
		std::cout << word << "\t";
	}
	if (right != NULL)	right->inTaversal();
}

void TNode::preTaversal(unsigned int k)
{
	for (unsigned int i = 0; i < k; i++)
	{
		std::cout << "\t";
	}
	for (int i = 0; i < count; i++)
	{
		std::cout << word ;
	}
	std::cout << std::endl;
	if (left != NULL)	left->preTaversal(k+1);
	if (right != NULL)	right->preTaversal(k+1);
}

void TNode::tailTaversal()
{
	if (left != NULL)	left->tailTaversal();
	if (right != NULL)	right->tailTaversal();
	for (int i = 0; i < count; i++)
	{
		std::cout << word << "\t";
	}
}

void TNode::deleteNode()
{
	if (left != NULL)	left->deleteNode();
	if (right != NULL)	right->deleteNode();
	delete[] this->word;
}
